import { ref, shallowRef } from 'vue'

import { getRechargeList } from './api'
import { catchFn } from '@/libs/common'
import { pay } from '../data'

/** VIP套餐列表 */
const vipList = shallowRef<T_PayItemVip[]>([])
/** 充值项列表 */
const itemList = shallowRef<T_PayItemRecharge[]>([])
/** 当前选中VIP套餐 */
const curVip = ref(-1)
/** 当前选中充值项 */
const curItem = ref(-1)

export { vipList, itemList, curVip, curItem }

/**
 * 更新用户详情
 */
const update = () =>
  catchFn(async () => {
    const { data } = await getRechargeList()

    vipList.value = data.vipItemList
    itemList.value = data.rechargeItemList

    itemList.value.length && (curItem.value = 0)
  })

/**
 * 确认支付
 */
const confirm = (item: T_PayItemVip | T_PayItemRecharge) => pay({ itemId: item.id })

/**
 * 清空
 */
const clear = () => {
  vipList.value = []
  itemList.value = []
  curVip.value = -1
  curItem.value = -1
}

export { update, confirm, clear }
